package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Special_bulletin;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.Special_bulletinService;
import com.csr.csrcheck.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@RequestMapping({"specialb"})
@RestController
public class Special_bulletinController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.Special_bulletinController.class);

    @Resource
    private Special_bulletinService special_bulletinService;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Resource
    private CompanyService companyService;

    @RequestMapping(path = {"addspecialb"}, method = {RequestMethod.POST})
    public JsonResult<Object> addspecialb(Special_bulletin special_bulletin, @RequestParam("release_time") String date) throws ParseException {
        special_bulletin.setRelease_time(sdf.parse(date));
        this.special_bulletinService.addSpecial_bulletin(special_bulletin);
        log.info("add================================>approvals"+ this.special_bulletinService);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"listcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> listcompany() {
        List list = this.companyService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"updatespecial"}, method = {RequestMethod.POST})
    public JsonResult<Void> updatespecial(Special_bulletin special_bulletin) {
        this.special_bulletinService.updateSpecial_bulletin(special_bulletin);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findSpecialByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Special_bulletin> findStock_alterationByid(@PathVariable("id") int id) {
        Special_bulletin list = null;
        try {
            list = this.special_bulletinService.findspecial_bulletinByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"deletespeciall/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deletespecial(@PathVariable("id") int id) {
        this.special_bulletinService.deletespecial_bulletinByid(id);
        log.info(""+ id);
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
